using UnityEngine;
using QFramework;
using System.Collections.Generic;

namespace ProjectSurvivor
{
	public partial class SimpleCircle : ViewController//表示一个挂在场景物体上的控制器（如玩家、刷怪器）。
	{
		public List<Collider2D> mSword = new List<Collider2D>();
		public BindableProperty<bool> SupperRotateSword = new BindableProperty<bool>(false);

		void Start()
		{
			Circle.Hide();
			Global.SimpleSwordCount.RegisterWithInitValue(count =>
			{
				var toAddCount = count - mSword.Count;
				for (int i = 0; i < toAddCount; i++)
				{
					if (Player.Default)
					{
						if (count > 10)
						{
							SupperRotateSword.Value = true;
						}
						if (Circle)
						{
							mSword.Add(Circle.InstantiateWithParent(this).Show().Position(Player.Default.Position()).Self(self =>
						{
							self.OnTriggerEnter2DEvent(collider =>
							{
								var HurtBox = collider.GetComponent<HurtBox>();
								if (HurtBox)
								{
									if (HurtBox.Owner.CompareTag("Enemy"))
									{
										var damageTimes = SupperRotateSword.Value ? Random.Range(2, 3 + 1) : 1;
										if (HurtBox.Owner.GetComponent<Enemy>())
										{
											DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value * damageTimes, HurtBox.Owner.GetComponent<Enemy>());
										}
										else
										{
											DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value * damageTimes, HurtBox.Owner.GetComponent<EnemyMinBoss>());
										}
										if (Random.Range(0, 1f) < 0.5f)
										{
											collider.attachedRigidbody.velocity = collider.NormalizedDirection2DFrom(self) * 5 + collider.NormalizedDirection2DFrom(Player.Default) * 10;
										}
									}
								}
							}).UnRegisterWhenGameObjectDestroyed(gameObject);
						}));
						}
						
					}


				}
				UndateCirclePos();
			});
			Global.SimpleSwordRange.Register(range =>
			{
				UndateCirclePos();
			});
			
		}
		void UndateCirclePos()
		{
			var radius = 3;
			var durationDegreea = 360 / mSword.Count;
			for (int i = 0; i < mSword.Count; i++)
			{
				if (mSword[i] != null)
				{
					var CirleLocalPos = new Vector2(Mathf.Cos(durationDegreea*i * Mathf.Deg2Rad), Mathf.Sin(durationDegreea*i * Mathf.Deg2Rad)) * radius;
					mSword[i].LocalPosition(CirleLocalPos.x, CirleLocalPos.y)
					.LocalEulerAnglesZ(durationDegreea*i-90);
				}
			}
		}
		void Update()
		{
			var speed = SupperRotateSword.Value ? Time.frameCount * 10 : Global.SimpleSwordSpeed.Value*Time.frameCount;
			//var degree = Time.frameCount*Global.SimpleSwordSpeed.Value*speedTimes;//（从游戏启动到当前帧的累计帧数）。
			//var degree = speedTimes;
			this.LocalEulerAnglesZ(-speed);
			UndateCirclePos();
		}
	}
}
